/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dato;

/**
 *
 * @author Omar Heráclides
 */
import Objetos.Personas;
import Objetos.Rol;
import Utilitarios.Conexion;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.*;

public class DRol {

    static ResultSet rs = null;
    static Statement st = null;

    public static ResultSet Select(String query) throws SQLException {

        Conexion conex = new Conexion();
        st = conex.ObtenerConexion();
        rs = st.executeQuery(query);
        return rs;
    }

    public static void Insert(String query) throws SQLException {

        Conexion conex = new Conexion();
        st = conex.ObtenerConexion();
        st.execute(query);
        st.close();
    }

    public static void UpDate(String query) throws SQLException {

        Conexion conex = new Conexion();
        st = conex.ObtenerConexion();
        st.execute(query);
        st.close();
    }

    public DefaultTableModel MostrarRol() throws SQLException {
        System.out.println("entrooo");
        DefaultTableModel dtm = new DefaultTableModel();
        String titulos[] = {"Codigo", "Descripción", "Nombre"};
        dtm.setColumnIdentifiers(titulos);

        Object datos[] = new Object[3];
        ResultSet rs;
        rs = Select("select * from rol");


        while (rs.next()) {
            datos[0] = (String) rs.getString(1);
            datos[1] = rs.getString(2);
            datos[2] = rs.getString(3);
            dtm.addRow(datos);
            System.out.println("mostrar" + datos[2]);


        }
        return dtm;

    }

    public DefaultTableModel LimpiarTabla() {
        DefaultTableModel dtm = new DefaultTableModel();
        String titulos[] = {"Codigo", "Nombre", "Descripción"};
        dtm.setColumnIdentifiers(titulos);
        Object datos[] = new Object[3];
        dtm.addRow(datos);
        return dtm;
    }

    public void AgregarRol(Rol objRol) throws SQLException {

        String comando;
        comando = "insert INTO rol (idrol,idusuario, descripcion,nombre) values ";
        comando = comando + "(" + objRol.getIdRol() + "," + Integer.parseInt(objRol.getIdUsuario()) + ",'" + objRol.getDescripcion() + "','" + objRol.getNombre() + "')";
        JOptionPane.showMessageDialog(null, comando, "Información", JOptionPane.INFORMATION_MESSAGE);
        Insert(comando);
    }

    public void ModificarRol(Rol objRol) throws SQLException {
        String comando;

        comando = "UPDATE rol SET descripcion='" + objRol.getDescripcion() + "', nombre='" + objRol.getNombre() + "'";
        comando = comando + " WHERE idrol=" + objRol.getIdRol();
        //JOptionPane.showMessageDialog(null, comando, "Información", JOptionPane.INFORMATION_MESSAGE);
        UpDate(comando);
    }

    public void EliminarRol(int cod) throws SQLException {
        String comando;
        comando = "Delete from rol WHERE idrol=" + cod + "";
        UpDate(comando);
    }

    public int ObtenerIdRol() throws SQLException {
        String comando;
        comando = "Select count(*) from rol";
        int num = 0;
        ResultSet rs = Select(comando);
        while (rs.next()) {
            num = Integer.parseInt(rs.getString(1));
        }
        num++;
        System.out.println(num);
        return num;
    }

    public ArrayList<String> ObtenerRol() throws SQLException {

        ArrayList<String> arr = new ArrayList();
        ResultSet rs;
        rs = Select("select nombre from rol");
        while (rs.next()) {
            arr.add((String) rs.getString(1));
        }
        return arr;
    }

    public String ObtenerIDROLNAME(String rolName) throws SQLException {
        String comando;
        System.out.println("name=" + rolName);
        comando = "Select idrol from rol where nombre='" + rolName + "'";
        String num = "";
        ResultSet rs = Select(comando);
        while (rs.next()) {
            num = rs.getString(1);
            num.trim();
        }

        System.out.println(num);
        return num;
    }

    public ArrayList<String> ObtenerRolxPerfil(String name) throws SQLException {
        String comando;
        ArrayList<String> arr = new ArrayList();
        String idRol = this.ObtenerIDROLNAME(name);
ArrayList<String> arrVentanas = new ArrayList();
        System.out.println("idrol=" + idRol);
        comando = "Select idpermiso from rolxpermiso where idrol='" + idRol.trim() + "'";
        System.out.println("comando="+comando);
        String num = "";
        ResultSet rs = Select(comando);
        while (rs.next()) {
            num = rs.getString(1);
            num.trim();
            System.out.println("ipermisooooo-"+num+"-");
            arr.add(num);
        }
        DPermiso objDPermiso = new DPermiso();
        for (int i = 0; i < arr.size(); i++) {
            arrVentanas.add(objDPermiso.ObtenerPermisoConIDPermiso(arr.get(i)));
        }
        return arrVentanas;
    }

    
}
